package com.daervin.svc.common.handler;

import com.daervin.svc.common.dto.UserDTO;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by daervin on 2023/8/26
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 获取登录用户信息
        UserDTO user = (UserDTO) request.getSession().getAttribute("user");
        if (user == null) {
            // 未登录，返回登录页面
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }
}